package com.chinairi.mall.service.impl;

import com.chinairi.mall.constant.CodeHeaderEnum;
import com.chinairi.mall.dao.CodeGeneratorMapper;
import com.chinairi.mall.service.ICodeGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 工业电商项目自动生成编码业务逻辑层实现类
 * @author lichangci
 * @since 2020-08-19
 */
@Service
public class CodeGeneratorServiceImpl implements ICodeGeneratorService {

    @Autowired
    private CodeGeneratorMapper codeGeneratorMapper;

    @Override
    public String getReqModelNo() {
        String header = CodeHeaderEnum.REQ_MODEL.getCodeValue();
        return header + getDate(new Date()) + numberFormat(4, codeGeneratorMapper.getReqModelNo());
    }

    /**
     * 转换日期类型
     * @param date 需要转换的日期
     * @return 返回转换后的日期信息
     */
    private String getDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date).replaceAll("-", "");
    }

    /**
     * 根据规则生成流水号
     * @param numberDigit ：流水号的位数
     * @param numberValue：流水号的值
     * @return 流水号的值的字符串信息
     */
    private String numberFormat(int numberDigit, int numberValue) {
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(numberDigit);
        formatter.setGroupingUsed(false);
        return formatter.format(numberValue);
    }
}
